var exec = require('child_process').exec
var fs = require('fs')
var path = require('path')
var del = require('del')
var gulp = require('gulp')
var child_process = require('child_process')

/**
 * 执行命令
 * @param {string} cmd
 */
function execPromise(cmd) {
  return new Promise((resolve, reject) => {
    console.log('开始打包: ' + cmd)
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        console.warn(error)
      }
      console.log('打包完成: ' + cmd)
      resolve(stdout ? stdout : stderr)
    })
  })
}

// 执行清理命令
gulp.task('clean', () => del(['./dist']))

// 执行编译命令
gulp.task('compile', function(done) {
  execPromise('npm run tsc').then(() => {
    console.log('服务端代码打包完成')
    done()
  })
})

// 执行安装插件命令
gulp.task('copy template', function() {
  return gulp.src('./templates').pipe(gulp.dest('./dist/templates'))
})

// 执行安装插件命令
gulp.task('copy public', function() {
  return gulp.src('./public').pipe(gulp.dest('./dist/public'))
})

gulp.task('build', gulp.series('clean', 'compile', 'copy template', 'copy public'))
